﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using YXShop.IDAL.SystemInfo;

namespace YXShop.SQLServerDAL.SystemInfo
{
    /// <summary>
    /// 数据访问类MouldInfo。
    /// </summary>
    public class MouldInfo : IMouldInfo
    {
        public MouldInfo()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from yxs_mouldinfo");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            return ChangeHope.DataBase.SQLServerHelper.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(YXShop.Model.SystemInfo.MouldInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into yxs_mouldinfo(");
            strSql.Append("MouldType,Name,WebPagePath,Description,Stat)");
            strSql.Append(" values (");
            strSql.Append("@MouldType,@Name,@WebPagePath,@Description,@Stat)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@MouldType", SqlDbType.Int,4),
					new SqlParameter("@Name", SqlDbType.NVarChar,200),
					new SqlParameter("@WebPagePath", SqlDbType.NVarChar,200),
					new SqlParameter("@Description", SqlDbType.NVarChar,200),
					new SqlParameter("@Stat", SqlDbType.Int,4)};
            parameters[0].Value = model.MouldType;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.WebPagePath;
            parameters[3].Value = model.Description;
            parameters[4].Value = model.Stat;

            object obj = ChangeHope.DataBase.SQLServerHelper.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(YXShop.Model.SystemInfo.MouldInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update yxs_mouldinfo set ");
            strSql.Append("MouldType=@MouldType,");
            strSql.Append("Name=@Name,");
            strSql.Append("WebPagePath=@WebPagePath,");
            strSql.Append("Description=@Description,");
            strSql.Append("Stat=@Stat");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@MouldType", SqlDbType.Int,4),
					new SqlParameter("@Name", SqlDbType.NVarChar,200),
					new SqlParameter("@WebPagePath", SqlDbType.NVarChar,200),
					new SqlParameter("@Description", SqlDbType.NVarChar,200),
					new SqlParameter("@Stat", SqlDbType.Int,4)};
            parameters[0].Value = model.Id;
            parameters[1].Value = model.MouldType;
            parameters[2].Value = model.Name;
            parameters[3].Value = model.WebPagePath;
            parameters[4].Value = model.Description;
            parameters[5].Value = model.Stat;

            ChangeHope.DataBase.SQLServerHelper.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from yxs_mouldinfo ");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            ChangeHope.DataBase.SQLServerHelper.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public YXShop.Model.SystemInfo.MouldInfo GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,MouldType,Name,WebPagePath,Description,Stat from yxs_mouldinfo ");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            YXShop.Model.SystemInfo.MouldInfo model = new YXShop.Model.SystemInfo.MouldInfo();
            DataSet ds = ChangeHope.DataBase.SQLServerHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MouldType"].ToString() != "")
                {
                    model.MouldType = int.Parse(ds.Tables[0].Rows[0]["MouldType"].ToString());
                }
                model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                model.WebPagePath = ds.Tables[0].Rows[0]["WebPagePath"].ToString();
                model.Description = ds.Tables[0].Rows[0]["Description"].ToString();
                if (ds.Tables[0].Rows[0]["Stat"].ToString() != "")
                {
                    model.Stat = int.Parse(ds.Tables[0].Rows[0]["Stat"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public YXShop.Model.SystemInfo.MouldInfo GetModelName(string Name)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,MouldType,Name,WebPagePath,Description,Stat from yxs_mouldinfo ");
            strSql.Append(" where Name=@Name ");
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.VarChar,200)};
            parameters[0].Value = Name;

            YXShop.Model.SystemInfo.MouldInfo model = new YXShop.Model.SystemInfo.MouldInfo();
            DataSet ds = ChangeHope.DataBase.SQLServerHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MouldType"].ToString() != "")
                {
                    model.MouldType = int.Parse(ds.Tables[0].Rows[0]["MouldType"].ToString());
                }
                model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                model.WebPagePath = ds.Tables[0].Rows[0]["WebPagePath"].ToString();
                model.Description = ds.Tables[0].Rows[0]["Description"].ToString();
                if (ds.Tables[0].Rows[0]["Stat"].ToString() != "")
                {
                    model.Stat = int.Parse(ds.Tables[0].Rows[0]["Stat"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        public System.Data.DataTable GetTypeList(string TypeId)
        {
            string Sql = "select * from yxs_mouldinfo where MouldType="+TypeId;
            return ChangeHope.DataBase.SQLServerHelper.Query(Sql).Tables[0];
        }

        public string GetList()
        {
            ChangeHope.WebPage.DataView dataview = new ChangeHope.WebPage.DataView();
            dataview.RowHead = "序号/5%,模型类别/10%,模型名称/20%,模型路径/30%,模型描述/20%,操作/15%";
            dataview.RowText = "{0}$@allnum,<a href='?w_d_MouldType={0}'>{1}</a>$MouldType|MouldTypeContent,Name,WebPagePath,Description,<a href='mouldinfo_edit.aspx?id={0}'>编辑</a> <a href='?w_d_MouldType={1}&action=del&id={0}' onclick='return confirm(\"是否删除该项\")'>删除</a>$Id|MouldType";
            dataview.Sql = "[select] *,(select name from yxs_mouldtype where id=yxs_mouldinfo.MouldType)as MouldTypeContent [from] yxs_mouldinfo [where] 1=1 [order by] Id asc";
            string view = dataview.GetView();
            dataview = null;
            return view;
        }
        #endregion  成员方法
    }
}

